package com.manyiaby.view.viewutils;

import android.app.Activity;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.TextView;

import com.manyiaby.R;

/**
 * Created by yx on 2017/10/18.
 * 使用中的展开、收起TextView功能
 */

public class MoreTextViewUtil {

    private static int defaultLine = 6; // 显示的行数,超过就隐藏
    private static boolean isExpands = true;//为true可展开，为false可收起

    public static boolean isPrompt = false;//使用文字按钮true，使用背景图片按钮false，为什么要设置呢？因为有可能你的TextView按钮不是个文字按钮，而是设置的图片背景按钮

    public interface mTVCallback {
        void onSuccess();
    }

    /**
     * @param activity     当前上下文Activity
     * @param textView     显示内容的View
     * @param openView     “显示全文”或“收起”View
     * @param str          内容String
     * @param defaultLines 默认显示行数
     * @param isPrompts    文字按钮还是图片按钮
     */
    public static void inItView(final Activity activity, final TextView textView, final TextView openView, final String str, int defaultLines, final boolean isPrompts, final mTVCallback callback) {
        //☆1
        defaultLine = defaultLines;
        isPrompt = isPrompts;
        textView.setText(str);
        final int count = textView.getLayout() == null ? getLineNumber(activity, textView)
                : textView.getLineCount();
        if (count > defaultLine) {
            textView.setLines(defaultLine);
            openView.setVisibility(View.VISIBLE);
        } else {
            openView.setVisibility(View.GONE);
        }
        //☆2
        // 监听显示或收起按钮事件
        openView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //AnimationUtils.scale(openView);
                inItView(activity, textView, openView, str, count, isPrompts, callback);//防止在滑动控件中时点击展开无法展开问题
                setIsExpand(textView, openView);
                if (callback != null) {
                    callback.onSuccess();
                }
            }
        });
    }

    // 计算内容文本框的占用的行数
    private static int getLineNumber(Activity activity, TextView textView) {
//		WindowManager wm = (WindowManager) getContext().getSystemService(
//				Context.WINDOW_SERVICE);
//		int width = wm.getDefaultDisplay().getWidth();
        DisplayMetrics dm = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        int width = dm.widthPixels;

        int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(width,
                View.MeasureSpec.AT_MOST);
        int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED);
        textView.measure(widthMeasureSpec, heightMeasureSpec);
        int lineHeight = textView.getLineHeight();
        int lineNumber = textView.getMeasuredHeight() / lineHeight;
        return lineNumber;
    }

    // 改变当前标记的值，并判断当前处于何种状态
    private static void setIsExpand(TextView textView, TextView openView) {
        if (isExpands) {//为true可展开
            isExpands = false;
            textView.setLines(textView.getLineCount());
            if (isPrompt) {
                openView.setText("收起");
                openView.setBackgroundResource(R.drawable.dr_transparent);
            }
        } else {//为false可收起
            isExpands = true;
            textView.setLines(defaultLine);
            if (isPrompt) {
                openView.setText("展开显示");
                openView.setBackgroundResource(R.drawable.dr_transparent);
            }
        }
    }

}
